package com.sanrenzu.chunbo.ui.fenlei;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.orhanobut.logger.Logger;
import com.sanrenzu.chunbo.R;
import com.sanrenzu.chunbo.adapter.LunBoAdapter;
import com.sanrenzu.chunbo.adapter.QiangGouAdapter;
import com.sanrenzu.chunbo.adapter.ReMaiAdapter;
import com.sanrenzu.chunbo.adapter.TuiJianAdapter;
import com.sanrenzu.chunbo.bean.zxb.GuanggaoLunboBean;
import com.sanrenzu.chunbo.bean.zxb.LaoTangChildBean;
import com.sanrenzu.chunbo.bean.zxb.QiangGouChildBean;
import com.sanrenzu.chunbo.bean.zxb.ReMaiChildBean;
import com.sanrenzu.chunbo.bean.zxb.TuiJianChildBean;
import com.sanrenzu.chunbo.ui.vertical.fenlei.FenLeiActivity;
import com.sanrenzu.chunbo.ui.vertical.gengduo.GengDuoActivity;
import com.sanrenzu.chunbo.widget.MyGirdView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by My on 2016/8/30.
 */
public class FenLeiFragment extends Fragment implements FenLeiContract.View {

    //轮播viewpager
    @BindView(R.id.fenlei_viewpager_fragment)
    ViewPager fenleiViewpagerFragment;
    @BindView(R.id.fenlei_btn)
    Button fenleiBtn;
    private LunBoAdapter lunBoAdapter;//其适配器

    //抢购

    @BindView(R.id.fenlei_gridview_fragment)
    MyGirdView fenleiGridviewFragment;
    private QiangGouAdapter qiangGouAdapter;

    //推荐
    @BindView(R.id.fenlei_gridviewtuijian_fragment)
    MyGirdView fenleiGridviewtuijianFragment;
    private TuiJianAdapter tuiJianAdapter;

    //热卖
    @BindView(R.id.fenlei_gridviewremai_fragment)
    MyGirdView fenleiGridviewremaiFragment;
    private ReMaiAdapter reMaiAdapter;

    //老汤
    @BindView(R.id.fenlei_listview_fragment_laotang)
    ImageView fenleiListviewFragmentLaotang;
    //persenter内部方法调用
    private FenLeiContract.Presenter fenLeiPresenter;
    // 黄油刀对象声明
    private Unbinder bind;

    //自动轮播
    private final int AUTO_MSG = 1;
    private final int HANDLE_MSG = AUTO_MSG + 1;
    private static final int PHOTO_CHANGE_TIME = 8000;//定时变量
    private int index = 0;
    private Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case AUTO_MSG:
                    fenleiViewpagerFragment.setCurrentItem(index++);//收到消息后设置当前要显示的图片
                    mHandler.sendEmptyMessageDelayed(AUTO_MSG, PHOTO_CHANGE_TIME);
                    if (index > 5) {
                        index = 0;
                    }
                    break;
                case HANDLE_MSG:
                    mHandler.sendEmptyMessageDelayed(AUTO_MSG, PHOTO_CHANGE_TIME);
                    break;
                default:
                    break;
            }
        }

        ;
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fenlei, container, false);
        bind = ButterKnife.bind(this, view);
        fenLeiPresenter = new FenLeiPresenter(this);
        initPersenter();
        return view;
    }

    //persenter的方法的初始化
    private void initPersenter() {
        fenLeiPresenter.getVertical();
        fenLeiPresenter.getVerticalQiangGouBean();
        fenLeiPresenter.getVerticalTuiJianBean();
        fenLeiPresenter.getVerticalReMaiBean();
        fenLeiPresenter.getVerticalLaoTangBean();
    }

    //view的回调，成功获取后展示的json数据   轮播的显示
    @Override
    public void onGetVerticalDataBean(List<GuanggaoLunboBean> dataBean) {
        Log.i("TAG", "-->>>onGetVerticalDataBean: " + dataBean);
        lunBoAdapter = new LunBoAdapter(getActivity(), dataBean);

        fenleiViewpagerFragment.setAdapter(lunBoAdapter);
        mHandler.sendEmptyMessageDelayed(AUTO_MSG, PHOTO_CHANGE_TIME);
    }

    //view的回调，成功获取后展示的json数据   抢购的显示
    @Override
    public void onGetVerticalQiangGouChildBean(List<QiangGouChildBean> qiangGouChildBean) {
        qiangGouAdapter = new QiangGouAdapter(getActivity(), qiangGouChildBean);
        fenleiGridviewFragment.setAdapter(qiangGouAdapter);
    }

    //view的回调，成功获取后展示的json数据   推荐的显示
    @Override
    public void onGetVerticalTuiJianChildBean(List<TuiJianChildBean> tuiJianChildBean) {
        tuiJianAdapter = new TuiJianAdapter(getActivity(), tuiJianChildBean);
        fenleiGridviewtuijianFragment.setAdapter(tuiJianAdapter);
    }

    //view的回调，成功获取后展示的json数据   热卖的显示
    @Override
    public void onGetVerticalReMaiChildBean(List<ReMaiChildBean> reMaiChildBean) {
        reMaiAdapter = new ReMaiAdapter(getActivity(), reMaiChildBean);
        fenleiGridviewremaiFragment.setAdapter(reMaiAdapter);
    }

    //view的回调，成功获取后展示的json数据   老汤的显示
    @Override
    public void onGetVerticalLaoTangChildBean(final List<LaoTangChildBean> laoTangChildBean) {
        Glide.with(getActivity()).load(laoTangChildBean.get(0).getImageUrl() + "").into(fenleiListviewFragmentLaotang);
        fenleiListviewFragmentLaotang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), FenLeiActivity.class);
                intent.putExtra("id",laoTangChildBean.get(0).getRefId()+"");
                startActivity(intent);
            }
        });
    }

    //view的回调， 失败后展示的数据
    @Override
    public void onGetVerticalFailed(String error) {
        Logger.d(error);
    }

    //黄油刀解绑
    @Override
    public void onDestroy() {
        super.onDestroy();
        bind.unbind();
        mHandler = null;
    }

    public void onClick(View view) {

    }

    @OnClick(R.id.fenlei_btn)
    public void onClick() {
        Intent intent = new Intent();
        intent.setClass(getActivity(), GengDuoActivity.class);
        startActivity(intent);
    }
}
